using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MetaObjects.Contracts;

namespace MetaObjects.Core
{
	public class EntityCollection<TEntity> : KeyedCollection<object, TEntity>, IEntityCollection<TEntity>
		where TEntity : IEntity
	{
		public EntityCollection()
		{
		}

		public EntityCollection(IEnumerable<TEntity> entities)
		{
			foreach (var entity in entities)
			{
				Add(entity);
			}
		}

		protected override object GetKeyForItem(TEntity item)
		{
			return item.Key;
		}

		object IEntityCollection.this[object key]
		{
			get { return this[key];  }
		}
	}

	public class EntityCollection<TKey, TEntity> : KeyedCollection<TKey, TEntity>, IEntityCollection<TEntity>
		where TEntity : IEntity<TKey>
	{
		public EntityCollection()
		{
		}

		public EntityCollection(IEnumerable<TEntity> entities)
		{
			foreach (var entity in entities)
			{
				Add(entity);
			}
		}

		protected override TKey GetKeyForItem(TEntity item)
		{
			return item.Key;
		}

		TEntity IEntityCollection<TEntity>.this[object key]
		{
			get { return this[(TKey)key]; }
		}

		object IEntityCollection.this[object key]
		{
			get { return this[(TKey) key]; }
		}

		bool IEntityCollection.Contains(object key)
		{
			return Contains((TKey)key);
		}
	}
}